package insight.web.controllers;

import insight.common.PropertyLoader;
import insight.miescor.annotations.Constants;
import insight.miescor.opp.domain.ICurrency;
import insight.miescor.opp.domain.ILocation;
import insight.web.delegates.AjaxJSONDelegate;
import insight.web.delegates.PrimaveraDelegate;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.primavera.common.value.ObjectId;
import com.primavera.integration.client.bo.object.Location;
import com.primavera.integration.client.bo.object.ProjectCode;

@Controller
public class PrimaveraActionsController extends GenericController {

	@RequestMapping("/loadCurrencies.htm")
	public ModelAndView loadCurrencies(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<ICurrency> currencies = PrimaveraDelegate.loadCurrencyList(helper
				.getSession());
		Gson gson = new Gson();
		AjaxJSONDelegate.setTextResponse(gson.toJson(currencies), response);
		return null;
	}

	@RequestMapping("/addNewCustomer.htm")
	public ModelAndView addNewCustomer(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		JSONObject jObj = readJSON(request);
		ProjectCode code = new ProjectCode(helper.getSession());
		int prjCodeTypeObjectId = PrimaveraDelegate.getProjectCodeTypeObjectId(
				helper.getSession(), Constants.ProjectFields.CUSTOMER_NAME);

		code.setCodeTypeObjectId(new ObjectId(prjCodeTypeObjectId));

		code.setCodeValue(jObj.getString("id"));
		if (!jObj.get("parentId").equals(""))
			code.setParentObjectId(new ObjectId(jObj.getInt("parentId")));
		code.setDescription(jObj.getString("name"));

		ObjectId objId = code.create();
		JSONObject jResult = new JSONObject().put("success", true)
				.put("id", objId.toInteger())
				.put("text", code.getDescription());
		AjaxJSONDelegate.setResponse(jResult, response);
		return null;
	}

	@RequestMapping("/addNewLocation.htm")
	public ModelAndView addNewLocation(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		JSONObject jObj = readJSON(request);
		System.out.println(jObj);
		Location location = new Location(helper.getSession());
		location.setName(jObj.getString("name"));
	
		if (jObj.has("street1") && jObj.get("street1") != null)
			location.setAddressLine1(jObj.getString("street1"));
		if (jObj.has("street2") && jObj.get("street2") != null)
			location.setAddressLine1(jObj.getString("street2"));
		if (jObj.has("city") && jObj.get("city") != null)
			location.setCity(jObj.getString("city"));
		if (jObj.has("state") && jObj.get("state") != null)
			location.setState(jObj.getString("state"));
		if (jObj.has("postalCode") && jObj.get("postalCode") != null)
			location.setPostalCode(jObj.getString("postalCode"));
		if (jObj.has("country") && jObj.get("country") != null)
			location.setCountryCode(jObj.getString("country"));

		location.setLatitude(0);
		location.setLongitude(0);
		ObjectId locId = location.create();
		JSONObject jResult = new JSONObject().put("success", true)
				.put("id", locId).put("text", jObj.getString("name"));
		AjaxJSONDelegate.setResponse(jResult, response);
		return null;
	}

	@RequestMapping("/loadLocations.htm")
	public ModelAndView loadLocations(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<ILocation> location = PrimaveraDelegate.loadLocationList(helper
				.getSession());
		Gson gson = new Gson();
		AjaxJSONDelegate.setTextResponse(gson.toJson(location), response);
		return null;
	}

	@RequestMapping("/getCountryList.htm")
	public ModelAndView getCountryList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println(PropertyLoader.getProperty(
				"primavera.country.list", ""));
		File file = new File(PropertyLoader.getProperty(
				"primavera.country.list", ""));
		JSONArray jAry=new JSONArray();
		if (file.exists()) {
			
			String []ary;
			for (String line : FileUtils.readLines(file)){
				ary=line.split("::");
				jAry.put(new JSONObject().put("text", ary[0]).put("value",ary[1]));
			}					
		}else{
			System.out.println("Country list file not found");
		}
		
		AjaxJSONDelegate.setResponse(jAry, response);
		return null;
	}

}
